<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Home\Controller;
use Home\Controller\CommonController;
/**
 * Description of ArrangementController
 *
 * @author Rung-PC
 */
class ArrangementController extends CommonController{
    public function indexae(){
        $this->display();
    }
    
    public function arrange(){
        if (!empty($_FILES)) {           
                 $user_name = session('user_name');
                 $info = $this->upload(['zip'],'/arrange/'.$user_name.'/',array('date', 'Y-m-d'));
                 $zip_name='./Uploads'.$info['import']['savepath'].$info['import']['savename'];      //上传后的zip包
//                 dump($zip_name);
                 vendor("Unzip.Unzip");
                $z = new \Unzip();            
                $z->unzip($zip_name,'./Uploads/arrange/'.$user_name.'/', true, true);                            //解压
                $file_name = './Uploads/arrange/'.$user_name.'/'.'arrange/arrange.xlsx';
                 if(file_exists($file_name)){                     
                     vendor("PHPExcel.PHPExcel");
                    $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
                    $objPHPExcel = $objReader->load($file_name,$encode='utf-8');
                    $sheet = $objPHPExcel->getSheet(0);                
                    $highestRow = $sheet->getHighestRow(); // 取得总行数
                    $ary=[];                   
                    if($highestRow > 3){
                        for($i=2;$i<=$highestRow;$i++)
                        {                       
                            $ary[]=['idname'=>trim($objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue()),'idcard'=>trim($objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue()),'number'=>trim($objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue())];      //提取数据
                        } 
                    }
                 
//                dump($ary);
                    if(!empty($ary)){
                        $dirName = './Uploads/arrange/'.$user_name.'/'.'arrange/idphoto';
                        if(file_exists($dirName)){
                            $filenames = scandir($dirName);                            //dump($filenames);                
                            if(count($filenames) > 2){
                                 $idinfo = [];
                                foreach($filenames as $sub=>$files){
                                    if($files != '.' && $files != '..'){
                                       foreach ($ary as $key => $value) {                                          // dump($files)       ;          
                                           $file = substr($files, 0,-6);                                
                                           if($file == $value['number']){                                               //dump($file);                                                                         
                                               rename($dirName.'/'.$files,$dirName.'/'.$value['idcard'].'.jpg');        
                                               $idinfo[] = $ary[$key];
                                           }
                                       }   
                                    }
                                }
                               if(!empty($idinfo)){
                                    $objPHPExcel = new \PHPExcel();
                                    $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);  
                                    $objActSheet = $objPHPExcel->getActiveSheet();

                                    // 水平居中（位置很重要，建议在最初始位置）
                                    $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                                    $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);   

                                    $objActSheet->setCellValue('A1', '姓名');
                                    $objActSheet->setCellValue('B1', '身份证号码');

                                    // 设置个表格宽度
                                    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(16);
                                    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);

                                    // 垂直居中
                                    $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                                    $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                                    foreach ($idinfo as $k => $v) {
                                            $k+=2;
                                            $objActSheet->setCellValue('A'.$k, $v['idname']);    
                                            $objActSheet->setCellValue('B'.$k, $v['idcard']); 
                                            // 表格高度
                                            $objActSheet->getRowDimension($k)->setRowHeight(20); 
                                    }
                                   
                                    $fileName .= "info.xlsx";
                                    $fileName = iconv("utf-8", "gb2312", $fileName);;
                                    //设置活动单指数到第一个表,所以Excel打开这是第一个表
                                    $objPHPExcel->setActiveSheetIndex(0);
                                    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');        //dump($objWriter);
                                    $objWriter->save('./Uploads/arrange/'.$user_name.'/'.'arrange/'.$fileName);                       //生成的Execl表格移動到指定目錄
                                    unlink($file_name);
                                    $this->delDirAndFile('./Uploads/arrange/'.$user_name.'/'.date('Y-m-d', time()).'/');
//                                    exit;
                                    vendor('FileToZip.FileToZip');
                                    $dirIdphoto = './Uploads/arrange/'.$user_name.'/arrange/idphoto/';
                                    $handler = opendir($dirIdphoto);    //$cur_file 文件所在目录
                                    $download_file = array();
                                    $i = 0;
                                    while( ($filename = readdir($handler)) !== false ) {
                                        if($filename != '.' && $filename != '..') {
                                            $download_file[$i++] = $filename;
                                        }
                                    }
                                    closedir($handler);
                                    $scandir=new \traverseDir($dirIdphoto,'./Uploads/arrange/'.$user_name.'/arrange/');    //$save_path zip包文件目录
                                    $scandir->tozipIdphoto($download_file,$dirIdphoto);
                                    
                                    $arrangeDir = './Uploads/arrange/'.$user_name.'/arrange/';
                                    $handler = opendir($arrangeDir);    //$cur_file 文件所在目录
                                    $download_file = array();
                                    $i = 0;
                                    while( ($filename = readdir($handler)) !== false ) {
                                        if($filename != '.' && $filename != '..') {
                                            $download_file[$i++] = $filename;
                                        }
                                    }
                                    closedir($handler);
                                    $scandir=new \traverseDir($arrangeDir,'./Uploads/arrange/'.$user_name.'/');    //$save_path zip包文件目录
                                    $scandir->tozip($download_file,$arrangeDir);
                               }else{
                                   $this->error('没有一张照片跟身份证号匹配!');
                               }                              
                            }else{
                                $this->error('idphoto文件是空的!');
                            }
                        }else{
                            $this->error('idphoto目录不存在!');
                        }
                    }else{
                        $this->error('arrange.xlsx文件无有效数据!');
                    }
                 }else{
                     $this->error('arrange.xlsx文件不存在');
                 }
            }else{
                $this->error("请选择上传的文件");
            }          
        }
        
        public function delInfo(){
                 $user_name = session('user_name');
                 $info = $this->upload(['xlsx'],'/delxlsx/'.$user_name.'/',array('date', 'Y-m-d'));
                 $file_name='./Uploads'.$info['del']['savepath'].$info['del']['savename'];      //上传后的xlsx
//                 dump($zip_name);
                 if(file_exists($file_name)){                     
                     vendor("PHPExcel.PHPExcel");
                    $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
                    $objPHPExcel = $objReader->load($file_name,$encode='utf-8');
                    $sheet = $objPHPExcel->getSheet(0);                
                    $highestRow = $sheet->getHighestRow(); // 取得总行数
                    $ary=[];                   
                    if($highestRow > 3){
                        for($i=2;$i<=$highestRow;$i++)
                        {                       
                            $ary[]=trim($objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue());      //提取数据
                        } 
                    }
//                    dump($ary);
                    if(!empty($ary)){
                        $admin_user = D('AdminUser');
                        $data['idcard'] = ['in',$ary];
                        $result = $admin_user->del('identity_information',$data);
                        $idphoto = './Public/images/idphoto/';
                        $num = 0;
                        foreach ($ary as $value) {
                            $prefix = substr($value,0,2);                           // dump($prefix);
                            $img = $idphoto.$prefix.'/'.$value.'.jpg';                            //dump($img);
                            $bool = unlink($img);
                            if($bool){
                                $num++;
                            }
                        }
                        $this->error("数据库删除".$result.'条数据<br>图库删除'.$num.'张照片','',10);
                    }
                 }
        }
}
